package com.example.sfm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.StudentRecordsDto;
import com.example.sfm.pojo.entity.StudentRecords;
import com.example.sfm.result.Result;
import com.example.sfm.service.IStudentRecordsService;
import com.example.sfm.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@RestController("userStudentRecordsController")
@RequestMapping("/user/student-records")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "学生档案相关接口", description = "学生档案相关接口")
public class StudentRecordsController {
    private final IStudentRecordsService recordsService;

    /**
     * 创建学生档案
     */
    @PostMapping("/create")
    @Operation(summary = "创建学生档案", description = "创建学生档案")
    public Result<?> createRecord(@RequestBody @Validated @Parameter(description = "学生档案信息") StudentRecordsDto dto) {
        log.info("创建学生档案: {}", dto);
        recordsService.createRecord(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 更新档案信息
     */
    @PutMapping("/update/{id}")
    @Operation(summary = "更新档案信息", description = "更新档案信息")
    public Result<?> updateRecord(@PathVariable @Parameter(description = "学生档案ID",in = ParameterIn.PATH) Integer id,
                                  @Parameter(description = "更新内容") @RequestBody @Validated StudentRecordsDto dto) {
        log.info("更新档案: ID={}, 内容={}", id, dto);
        recordsService.updateRecord(id, dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }


    @GetMapping("/infoRecord")
    @Operation(summary = "获取档案信息", description = "获取档案信息")
    public Result<?> infoRecord() {
        log.info("档案信息获取");
        Integer id = ThreadLocalUtil.getId();
        return Result.success(recordsService.finByUserId(id), MessageConstants.SELECT_OK);
    }

    @GetMapping("/getClassName")
    @Operation(summary = "获取班级名称", description = "获取班级名称")
    public Result<?> getClassName() {
        log.info("学生获取班级名称");
        return Result.success(recordsService.getOne(new LambdaQueryWrapper<StudentRecords>()
                .eq(StudentRecords::getUserId, ThreadLocalUtil.getId())).getClassName(), MessageConstants.SELECT_OK);
    }

}
